#include "ffendevent.h"

FFEndEvent::FFEndEvent(FFPlayerContext *playerCtx)
    :FFEvent(playerCtx)
{
    demuxerThread = playerCtx->demuxerThread;
    vDecoderThread = playerCtx->vDecoderThread;
    aDecoderThread = playerCtx->aDecoderThread;
    vRender = playerCtx->vRender;
    demuxer = playerCtx->demuxer;
    aDecoder = playerCtx->aDecoder;
    vDecoder = playerCtx->vDecoder;
    aPktQueue = playerCtx->aPktQueue;
    vPktQueue = playerCtx->vPktQueue;
    aFrmQueue = playerCtx->aFrmQueue;
    vFrmQueue = playerCtx->vFrmQueue;
    playerWindow = playerCtx->playerWindow;
}

FFEndEvent::~FFEndEvent()
{

}

void FFEndEvent::work()
{
    demuxerThread->stop();
    demuxerThread->wakeAllThread();
    demuxerThread->wait();
    demuxerThread->close();

    std::cerr<<"demuxerThread stop!"<<std::endl;

    aRender->wakeAllThread();
    vRender->wakeAllThread();

    aDecoderThread->stop();
    aDecoderThread->wait();
    aDecoderThread->close();

    std::cerr<<"aDecoderThread stop!"<<std::endl;
    vDecoderThread->stop();
    vDecoderThread->wait();
    vDecoderThread->close();
    std::cerr<<"vDecoderThread stop!"<<std::endl;

    aPktQueue->close();
    vPktQueue->close();
    std::cerr<<"aPktQueue stop!"<<std::endl;

    aFrmQueue->close();
    vFrmQueue->close();
    std::cerr<<"aFrmQueue stop!"<<std::endl;

    aRender->stop();
    aRender->wait();
    aRender->close();
    std::cerr<<"aRender stop!"<<std::endl;

    vRender->stop();
    vRender->wait();
    vRender->close();
    std::cerr<<"vRender stop!"<<std::endl;

    QMetaObject::invokeMethod(playerWindow,"setBlackScreen",Qt::QueuedConnection);
}
